package com.onlyyou.custom.service.Impl;

import com.onlyyou.custom.entity.Custom;
import com.onlyyou.custom.enums.ResultEnum;
import com.onlyyou.custom.exception.CustomException;
import com.onlyyou.custom.mapper.CustomMapper;
import com.onlyyou.custom.service.CustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @Author: 小野
 * @Date: 2020/1/15 10:40
 */
@Service
public class CustomServiceImpl implements CustomService {

    @Autowired
    private CustomMapper customMapper;

    @Override
    public List<Custom> findAll(Integer pages, Integer rows) {
        return customMapper.findAll(pages,rows);
    }

    @Override
    public List<Custom> findByUserId(Long userId) {
        List<Custom> customs = customMapper.findByUserId(userId);
        if (null==customs || customs.isEmpty())
            throw new CustomException(ResultEnum.NULL);
        return customs;
    }

    @Override
    public List<Custom> findByOrderUserId(Long orderUserId) {
        List<Custom> customs = customMapper.findByOrderUserId(orderUserId);
        if (null==customs || customs.isEmpty())
            throw new CustomException(ResultEnum.NULL);
        return customs;
    }

    @Override
    public Custom findById(Long id) {
        Custom custom=customMapper.findById(id);
        if(null==custom)
            throw new CustomException(ResultEnum.NULL);
        return custom;
    }


    @Override
    @Transactional
    public Custom save(Custom custom) {
        customMapper.save(custom);
        Custom customSave = findById(custom.getId());
        if (null == customSave)
            throw new CustomException(ResultEnum.SAVE_ERROR);
        return customSave;
    }

    @Override
    public void delete(Custom custom) {
        customMapper.delete(custom);
    }
}
